import { defineStore } from 'pinia'
import $request from '@/fetch/request.js'

export const useUserStore = defineStore('user', {
  state: () => ({
    menuList: [], // 用户菜单列表
    permissionList: [], // 用户按钮列表
    info: {
      state: null,
      userAccount: '',
      userBirthday: null,
      userEmail: '',
      userMobile: '',
      userName: '',
      userNo: '',
      userPhotoList: null,
      userSex: null,
      userType: null,
    }, // 用户信息
    loginStatus: false, // 登录状态
    communityList: [],
    communityId: null,
  }),
  getters: {},
  actions: {
    async getUserInfo() {
      const res = await $request.get('/api-parking/getInfo')
      const { user } = res
      if (user.userType === '1') {
        this.menuList = [
          { resourceKey: 'Community', resourceName: '小区管理' },
          { resourceKey: 'Parking', resourceName: '车位管理' },
          { resourceKey: 'Order', resourceName: '订单管理' },
          { resourceKey: 'Reservation', resourceName: '临停预约管理' },
          { resourceKey: 'Settled', resourceName: '入驻申请' },
          { resourceKey: 'Owners', resourceName: '业主管理' },
          { resourceKey: 'Activity', resourceName: '活动管理' },
          { resourceKey: 'Prize', resourceName: '中奖管理' },
          { resourceKey: 'Reward', resourceName: '奖励管理' },
          { resourceKey: 'News', resourceName: '资讯管理' },
          { resourceKey: 'Evaluate', resourceName: '评价管理' },
          { resourceKey: 'Business', resourceName: '询价商机' },
          { resourceKey: 'User', resourceName: '人员管理' },
        ]
      } else if (user.userType === '3') {
        this.menuList = [
          { resourceKey: 'Parking', resourceName: '车位管理' },
          { resourceKey: 'Order', resourceName: '订单管理' },
          { resourceKey: 'Reservation', resourceName: '临停预约管理' },
          { resourceKey: 'Settled', resourceName: '入驻申请' },
          { resourceKey: 'Owners', resourceName: '业主管理' },
          { resourceKey: 'Activity', resourceName: '活动管理' },
          { resourceKey: 'Prize', resourceName: '中奖管理' },
          { resourceKey: 'Reward', resourceName: '奖励管理' },
          { resourceKey: 'News', resourceName: '资讯管理' },
          { resourceKey: 'Evaluate', resourceName: '评价管理' },
          { resourceKey: 'Business', resourceName: '询价商机' },
        ]
      } else {
        this.menuList = [
          { resourceKey: 'Parking', resourceName: '车位管理' },
          { resourceKey: 'Owners', resourceName: '业主管理' },
          { resourceKey: 'Business', resourceName: '询价商机' },
        ]
      }
      // this.menuList = menuList
      // this.permissionList = permissionList

      this.info = user
      this.loginStatus = true
      this.info.userPhotoList = user.avatar ? [user.avatar] : null
    },
    async getcommunityList() {
      const communityRes = await $request.get('/api-parking/parking/community/findCommunityByUserId')
      this.communityList = (communityRes || []).map((i) => ({ label: i.name, value: i.id }))
      if (this.communityList.length > 0) {
        this.communityId = this.communityList[0].value
      }
    },
    changeCommunity(value) {
      this.communityId = value
    },
  },
  persist: {
    enabled: true,
  },
})
